<template>
<Card>
  <p slot="title">
    审计复核
  </p>
  <Row type="flex" justify="center" align="middle">
    <Col span="3"> 复核结果
    </Col>
    <Col span="21">
    <Select v-model="resultOption" @on-change="resultOptionChange" label-in-value style="width:200px;">
              <Option v-for="item in resultOptions" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
    </Col>
    <!-- <Col span="6">
              资金来源
          </Col>
          <Col span="6">
              {{fundName}}
          </Col> -->
  </Row>
  <br>
  <Collapse value="1" v-if="resultOption == 'OK'">
    <Panel name="1">
      审批确认
      <p slot="content">
        <Form label-position="top">
          <FormItem label="审批确认" class="labelWeightBold">
            <CheckboxGroup v-model="samplingConfirms">
              <Checkbox :label="child.opinionTypeCode" v-for="child in samplingConfirmsSelect" :key="child.id">
                <span>{{child.opinionTypeName}}</span>
              </Checkbox>
            </CheckboxGroup>
          </FormItem>
          <FormItem label="审批意见" class="labelWeightBold">
            <Input v-model="samplingOpinionContent" type="textarea" :autosize="{minRows: 5,maxRows: 5}" placeholder="请输入内容"></Input>
          </FormItem>
          <FormItem label="违例代码" class="labelWeightBold">
            <Row>
              <Col span="12">
              <Select v-model="samplingViolationOne" style="width:300px" @on-change="violationOneChange" label-in-value>
                            <Option v-for="item in samplingViolationOneOptions" :value="item.opinionTypeCode" :key="item.id">{{ item.opinionTypeName }}</Option>
                        </Select>
              </Col>
              <Col span="12">
              <Select v-model="samplingViolationTwo" style="width:300px" label-in-value>
                            <Option v-for="item in samplingViolationTwoOptions" :value="item.opinionTypeCode" :key="item.id">{{ item.opinionTypeName }}</Option>
                        </Select>
              </Col>
            </Row>
          </FormItem>
          <FormItem label="违例意见" class="labelWeightBold">
            <Input v-model="samplingViolationOpinionContent" type="textarea" :autosize="{minRows: 5,maxRows: 5}" placeholder="请输入内容"></Input>
          </FormItem>
        </Form>
      </p>
    </Panel>
  </Collapse>
  <Collapse value="1" v-if="resultOption == 'RETURN'">
          <Panel name="1">
              复核结果
              <p slot="content">
                <Form label-position="top">
                  <FormItem label="退回原因" class="labelWeightBold">
                    <Input v-model="returnReason" type="textarea" :autosize="{minRows: 5,maxRows: 5}" placeholder="请输入内容"></Input>
                  </FormItem>
                </Form>
              </p>
          </Panel>
      </Collapse>
  <Collapse value="1" v-if="resultOption == 'RETURN_APP'">
    <Panel name="1">
      复核结果
      <p slot="content">
        <Form label-position="top">
          <FormItem label="退回原因大类" class="labelWeightBold">
            <CheckboxGroup v-model="samplingReturnAppOpinions">
              <Checkbox :label="child.opinionTypeCode" v-for="child in samplingReturnAppOpinionsSelect" :key="child.id">
                <span>{{child.opinionTypeName}}</span>
              </Checkbox>
            </CheckboxGroup>
          </FormItem>
          <FormItem label="退补件意见" class="labelWeightBold">
            <Input v-model="samplingReturnAppOpinionContent" type="textarea" :autosize="{minRows: 5,maxRows: 5}" placeholder="请输入内容"></Input>
          </FormItem>
        </Form>
      </p>
    </Panel>
  </Collapse>
  <Collapse value="1" v-if="resultOption == 'REFUSE'">
    <Panel name="1">
      复核结果
      <p slot="content">
        <Form label-position="top">
          <FormItem label="拒绝代码" class="labelWeightBold">
            <div>
              <Button type="info" size="small" @click="openOpinionsModal">选择拒绝意见</Button>
            </div>
            <Tag color="primary" v-for="item in choosedOpinionTypesLv3" :key="item.id" :name="item.opinionTypeCode">{{ item.opinionTypeName }}</Tag>
          </FormItem>
          <FormItem label="拒绝意见" class="labelWeightBold">
            <Input v-model="samplingRefuseOpinionContent" type="textarea" :autosize="{minRows: 5,maxRows: 5}" placeholder="请输入内容"></Input>
          </FormItem>
        </Form>
      </p>
    </Panel>
  </Collapse>
  <br>
  <div style="text-align:center">
    <Button type="primary" @click="saveOpinionBt" :disabled="taskParams.type!=1" >保存</Button>
    <Button type="primary" @click="complateOpinionBt" :disabled="taskParams.type!=1" >提交</Button>
  </div>
  <!-- <Table border :columns="samplingOpinionCols" :data="samplingOpinionDatas"></Table> -->
  <br>
  <Collapse value="1">
    <Panel name="1">
      审批流程
      <p slot="content">
        <Timeline>
          <TimelineItem v-for="(item,index) in samplingOpinionDatas" :key="item.node_name+'_'+index">
            <p class="common-title">{{item.updatedDate}}</p>
            <p class="common-title">{{item.nodeName}}</p>
            <p class="common-content" v-if="item.caseResult=='OK'">复核结果：通过</p>
            <p class="common-content" v-if="item.caseResult=='REFUSE'">复核结果：拒绝</p>
            <p class="common-content" v-if="item.caseResult=='RETURN'">复核结果：退回</p>
            <p class="common-content" v-if="item.caseResult=='RETURN_APP'">复核结果：退回前端</p>
            <p class="common-content">类型名称：{{item.opinionTypeName}}</p>
            <p class="common-content" v-if="item.opinionClass==3">复核意见：{{item.opinion}}</p>
            <p class="common-content" v-if="item.opinionClass==1">违例意见：{{item.opinion}}</p>
            <p class="common-content" v-if="item.opinionClass==0">拒绝意见：{{item.opinion}}</p>
            <p class="common-content" v-if="item.opinionClass==4">退回APP意见：{{item.opinion}}</p>
            <p class="common-content" v-if="item.opinionClass==5">退回意见：{{item.opinion}}</p>
            <p class="common-content">处理人员：{{item.updatedBy}}</p>
          </TimelineItem>
        </Timeline>
      </p>
    </Panel>
  </Collapse>
  <modalRefuse :menuLv1List="samplingRefuseOpinionOneSelect" :data="choosedLv3" :display="modal1" @close="closeModal"></modalRefuse>
</Card>

<!-- <Card v-if="resultOption == 'REFUSE'">
                <Row>
                    <Col span="6">
                        拒绝代码
                    </Col>
                    <Col span="18">
                        <Card>
                             <CheckboxGroup v-model="samplingRefuseOneOpinions" @on-change="samplingRefuseOpinionOneSelectChange">
                                <Checkbox :label="sampling.opinionTypeCode" v-for="sampling in samplingRefuseOpinionOneSelect" :key="sampling">
                                    <span>{{approval.opinionSupCode}}-{{sampling.opinionTypeName}}</span>
                                </Checkbox>
                            </CheckboxGroup>
                        </Card>
                        <Card v-if="samplingRefuseOpinionTwoSelect.length>0">
                            <CheckboxGroup v-model="samplingRefuseTwoOpinions" @on-change="samplingRefuseOpinionTwoSelectChange">
                                <Checkbox :label="sampling.opinionTypeCode" v-for="sampling in samplingRefuseOpinionTwoSelect" :key="sampling">
                                    <span>{{approval.opinionSupCode}}-{{sampling.opinionTypeName}}</span>
                                </Checkbox>
                            </CheckboxGroup>
                        </Card>
                        <Card v-if="samplingRefuseOpinionThrSelect.length>0">
                            <CheckboxGroup v-model="samplingRefuseThrOpinions">
                                <Checkbox :label="sampling.opinionTypeCode" v-for="sampling in samplingRefuseOpinionThrSelect" :key="sampling">
                                    <span>{{approval.opinionSupCode}}-{{sampling.opinionTypeName}}</span>
                                </Checkbox>
                            </CheckboxGroup>
                        </Card>
                    </Col>
                </Row>
            </Card> -->
</template>

<script>
import modalRefuse from '../../components/modal-refuse-list.vue'

export default {
  name: 'loan-sampling-form',
  components: {
    modalRefuse
  },
  data () {
    return {
      loanId: '',
      fundName: null,
      modal1: false,
      // 复核结果
      resultOption: null,
      resultOptions: [{
        label: '通过',
        value: 'OK'
      },
      {
        label: '退回',
        value: 'RETURN'
      },
      {
        label: '退回到前端',
        value: 'RETURN_APP'
      },
      {
        label: '拒绝',
        value: 'REFUSE'
      }
      ],
      samplingOpinionCols: [{
        type: 'index'
      },
      {
        title: '流程阶段',
        key: 'nodeName'
      },
      {
        title: '处理结果',
        key: 'caseResult'
      },
      {
        title: '处理意见',
        key: 'opinion'
      },
      {
        title: '处理人',
        key: 'createdBy'
      },
      {
        title: '处理时间',
        key: 'endTimeForMat'
      }
      ],
      // 审批确认 ， 审批结果
      samplingConfirms: [],
      samplingConfirmsSelect: [],
      samplingOpinionContent: null,
      // 违例
      samplingViolationOne: null,
      samplingViolationOneOptions: [],
      samplingViolationTwo: null,
      samplingViolationTwoOptions: [],
      samplingViolationOpinionContent: null,
      returnReason: null, // 退回原因
      // 退回APP
      samplingReturnAppOpinions: [],
      samplingReturnAppOpinionsSelect: [],
      samplingReturnAppOpinionContent: null,
      // 拒绝
      samplingRefuseOpinionContent: null,
      samplingRefuseOneOpinions: [],
      samplingRefuseOpinionOneSelect: [],
      samplingRefuseTwoOpinions: [],
      samplingRefuseOpinionTwoSelect: [],
      samplingRefuseThrOpinions: [],
      samplingRefuseOpinionThrSelect: [],
      samplingRefuseOpinions: [],
      choosedLv3: [], // 选中的三级菜单选项
      choosedOpinionTypesLv3: [], // 选中的三级菜单对象
      // 操作记录
      samplingOpinionDatas: []
    }
  },
  methods: {
    openOpinionsModal () {
      if (this.resultOption === 'REFUSE') {
        this.modal1 = true
      }
    },
    closeModal (choosedData) {
      this.modal1 = false
      this.choosedOpinionTypesLv3 = choosedData
    },
    // 保存
    saveOpinionBt () {
      if (!this.resultOption) {
        this.$Message.warning('请选择审批结果')
        return
      }
      var params = this.setParams()
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/sampling/service/addsampling', params).then(re => {
        if (re.data.code === 'success') {
          this.$Message.success({
            title: '提示',
            content: re.data.data.loanId + '保存成功!',
            onOk () {}
          })
        } else {
          this.$Message.warning({
            title: '提示',
            content: re.data.data.loanId + '保存失败',
            onOk () {}
          })
        }
      }).catch(er => {})
    },
    setParams () {
      var params = {}
      params.loanId = this.loanId
      params.processResult = this.resultOption
      // 审批确认
      var processOpinions = []
      if (this.samplingConfirms.length !== 0) {
        for (var i = 0; i < this.samplingConfirms.length; i++) {
          processOpinions.push({
            opinionTypeCode: this.samplingConfirms[i]
          })
        }
      }
      params.processOpinions = processOpinions
      // 违例代码
      params.violationOpinion = this.samplingViolationTwo
      // 违例意见
      params.violationOptionContent = this.samplingViolationOpinionContent
      // 退回大类
      var returnAppOpinions = []
      if (this.samplingReturnAppOpinions.length !== 0) {
        for (let i = 0; i < this.samplingReturnAppOpinions.length; i++) {
          returnAppOpinions.push({
            opinionTypeCode: this.samplingReturnAppOpinions[i]
          })
        }
      }
      params.returnAppOpinions = returnAppOpinions
      // 退回大类原因
      params.returnAppOpinionContent = this.samplingReturnAppOpinionContent
      // 拒绝代码
      var refuseOpinionTypes = []
      for (let i = 0; i < this.choosedOpinionTypesLv3.length; i++) {
        refuseOpinionTypes.push({
          opinionTypeCode: this.choosedOpinionTypesLv3[i].opinionTypeCode
        })
      }
      params.samplingOpinionTypes = refuseOpinionTypes
      if (params.processResult === 'OK') {
        // 审批意见
        params.samplingOpinionContent = this.samplingOpinionContent
      } else if (params.processResult === 'REFUSE') {
        // 拒绝意见
        params.samplingRefuseOpinionContent = this.samplingRefuseOpinionContent
      } else if (params.processResult === 'RETURN') {
        // 退回原因
        params.samplingOpinionContent = this.returnReason
      }
      return params
    },
    handleSubmit (params) {
      var config = {
        title: '提示',
        content: '是否确认提交?',
        onOk: () => {
          this.doSubmit(params)
        }
      }
      this.$Modal.confirm(config)
    },
    // 提交符合
    complateOpinionBt () {
      if (!this.resultOption) {
        this.$Message.warning('请选择审批结果')
        return
      }
      var params = this.setParams()
      var validate = this.validSubmitData(params)
      if (validate) {
        this.handleSubmit(params)
      }
    },
    doSubmit (params) {
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/sampling/service/complateSampling', params).then(re => {
        if (re.data.code === 'success') {
          this.$Message.success({
            title: '提示',
            content: re.data.data.loanId + '提交复核成功!',
            onOk () {}
          })
          this.$router.back()
        } else {
          this.$Message.warning({
            title: '提示',
            content: re.data.data.loanId + '提交复核失败',
            onOk () {}
          })
        }
      }).catch(er => {})
    },
    // 校验提交数据
    validSubmitData (params) {
      var flag = true
      if (params.processResult === 'OK') {
        if (!params.processOpinions || params.processOpinions.length < 1) {
          this.$Message.warning('审批确认选项不能为空')
          flag = false
        }
      } else if (params.processResult === 'RETURN') {
        if (!params.samplingOpinionContent || params.samplingOpinionContent == null || params.samplingOpinionContent === '') {
          this.$Message.warning('退回原因不能为空')
          flag = false
        }
      } else if (params.processResult === 'RETURN_APP') {
        if (!params.returnAppOpinions || params.returnAppOpinions.length < 1) {
          this.$Message.warning('退回原因选项不能为空')
          flag = false
        }
      } else if (params.processResult === 'REFUSE') {
        if (!params.samplingOpinionTypes || params.samplingOpinionTypes.length < 1) {
          this.$Message.warning('拒绝意见选项不能为空')
          flag = false
        }
      }
      return flag
    },
    // 加载审批结果
    loadFormData (data) { // 0 拒绝  1违例  2复核意见 3审批确认   4退回app  5退回上个流程
      var vm = this
      if (data && data.length > 0) {
        var opinion = data[0]
        vm.resultOption = opinion.caseResult
        if (opinion.caseResult === 'OK') { // 通过
          data.forEach(item => {
            if (item.opinionClass === 3) { // 渲染审批确认checkbox
              vm.samplingOpinionContent = item.opinion
              vm.samplingConfirms.push(item.opinionTypeCode)
            } else if (item.opinionClass === 1) { // 渲染违例代码
              vm.samplingViolationOpinionContent = item.opinion
              for (let i = 0; i < vm.samplingViolationOneOptions.length; i++) { // 一级违例代码列表
                let violationOptionsLv2 = vm.samplingViolationOneOptions[i].opinionTypes
                for (let j = 0; j < violationOptionsLv2.length; j++) { // 二级违例代码列表
                  if (item.opinionTypeCode === violationOptionsLv2[j].opinionTypeCode) {
                    vm.samplingViolationOne = violationOptionsLv2[j].opinionSupCode
                    vm.samplingViolationTwoOptions = violationOptionsLv2
                    vm.samplingViolationTwo = item.opinionTypeCode
                    return
                  }
                }
              }
            }
          })
        } else if (opinion.caseResult === 'RETURN') {
          vm.returnReason = opinion.opinion
        } else if (opinion.caseResult === 'RETURN_APP') { // 退回到前端
          vm.samplingReturnAppOpinionContent = opinion.opinion
          data.forEach(item => {
            vm.samplingReturnAppOpinions.push(item.opinionTypeCode)
          })
        } else if (opinion.caseResult === 'REFUSE') { // 拒绝
          vm.samplingRefuseOpinionContent = opinion.opinion
          vm.choosedLv3 = [] // 选中的三级菜单
          data.forEach(item => {
            vm.choosedLv3.push(item.opinionTypeCode)
          })
        }
      }
    },
    // 复核确认change方法
    // samplingConfirmChange(checkbox){
    //     // console.log(checkbox);
    //     // console.log(this.samplingConfirms);
    //     var curCode = checkbox.target.value;
    //     if(checkbox.target.checked){//选中
    //         var isHas = this.checkExistOpinionTypeCode(this.samplingConfirms,curCode);
    //         if(!isHas){
    //             this.selectItemOpinionTypeCode(this.samplingConfirms,this.opinionTypes3,curCode);
    //         }
    //     }else{//取消
    //         this.samplingConfirms = this.removeItemOpinionTypeCode(this.samplingConfirms,curCode);
    //     }
    // },
    // 违例one
    violationOneChange (option) {
      var value = option.value
      this.samplingViolationOneOptions.forEach(item => {
        if (value === item.opinionTypeCode) {
          this.samplingViolationTwoOptions = item.opinionTypes
        }
      })
    },
    // 退回APP
    // samplingReturnAppOpinionChange(){
    //    console.log(option);
    // },
    // 拒绝one
    samplingRefuseOpinionOneSelectChange (opinionTypes) {
      this.samplingRefuseOpinionTwoSelect = []
      this.samplingRefuseOpinionOneSelect.forEach(item => {
        var itemCode = item.opinionTypeCode
        var oneItem = item.opinionTypes
        opinionTypes.forEach(item2 => {
          if (itemCode === item2) {
            this.samplingRefuseOpinionTwoSelect = this.samplingRefuseOpinionTwoSelect.concat(oneItem)
          }
        })
      })
    },
    // 拒绝two
    samplingRefuseOpinionTwoSelectChange (opinionTypes) {
      this.samplingRefuseOpinionThrSelect = []
      this.samplingRefuseOpinionTwoSelect.forEach(item => {
        var itemCode = item.opinionTypeCode
        var oneItem = item.opinionTypes
        opinionTypes.forEach(item2 => {
          if (itemCode === item2) {
            this.samplingRefuseOpinionThrSelect = this.samplingRefuseOpinionThrSelect.concat(oneItem)
          }
        })
      })
    },
    // 拒绝three
    // samplingRefuseOpinionThrSelectChange(opinionTypes){
    //     // console.log("拒绝" + opinionTypes)
    // },
    // 删除一个值
    // removeItemOpinionTypeCode(arrayData,curValue){
    //     var tems = [];
    //     arrayData.forEach(item=>{
    //          var code = item.opinionTypeCode;
    //         if(curValue == code){
    //            // console.log(code+"被删除");
    //         }else{
    //             tems.push(item);
    //         }
    //     });
    //     return tems;
    // },
    // 获取值一样的
    // selectItemOpinionTypeCode(sourceDatas,arrayData,curValue){
    //     arrayData.forEach(item=>{
    //         var code = item.opinionTypeCode;
    //         if(curValue == code){
    //             sourceDatas.push(item);
    //             return;
    //         }
    //     });
    // },
    // 存在返回true
    // checkExistOpinionTypeCode(arrayData,curValue){
    //     arrayData.forEach( item => {
    //         var code = item.opinionTypeCode;
    //         if(curValue == code){
    //             return true;
    //         }
    //     });
    //     return false;
    // },
    resultOptionChange (option) {
      // 审核通过
      this.samplingConfirms = []
      this.samplingOpinionContent = null
      // 违例代码
      this.samplingViolationOne = null
      this.samplingViolationTwo = null
      this.samplingViolationOpinionContent = null
      // 退回原因大类
      this.samplingReturnAppOpinions = []
      // 退补意见
      this.samplingReturnAppOpinionContent = null

      this.samplingRefuseOneOpinions = []
      this.samplingRefuseTwoOpinions = []
      this.samplingRefuseThrOpinions = []
      this.samplingRefuseOpinionContent = null
      this.choosedOpinionTypesLv3 = []
    },
    // 初始化
    initData () {
      var vm = this
      var params = {}
      params.loanId = this.loanId
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/sampling/service/querysampling', params).then(re => {
        if (re.data.code === 'success') {
          // 当前单据类型：是线上还是线下
          var patternCode = re.data.data.loanApplyCase.patternCode
          // 审批确认
          this.samplingConfirmsSelect = re.data.data.opinionTypes[3] || []
          // 违例代码
          var samplingViolationOneOptions = re.data.data.opinionTypes[1] || []
          samplingViolationOneOptions.forEach(item => {
            if (item.patternCode === patternCode) {
              vm.samplingViolationOneOptions.push(item)
            }
          })
          // 退回APP原因大类
          this.samplingReturnAppOpinionsSelect = re.data.data.opinionTypes[4] || []
          // 拒绝代码
          vm.samplingRefuseOpinionOneSelect = []

          var checkOpinionDatas = re.data.data.opinionTypes[0] || []
          // 获取当前单据类型的拒绝列表
          checkOpinionDatas.forEach(item => {
            if (item.patternCode === patternCode) {
              vm.samplingRefuseOpinionOneSelect.push(item)
            }
          })
          // 操作记录
          re.data.data.logs.forEach(item => {
            item.updatedDate = this.$util.formatTime(new Date(item.updatedDate))
            for (let i = 0; i < vm.resultOptions.length; i++) {
              if (vm.resultOptions[i].value === item.caseResult) {
                item.caseResultName = vm.resultOptions[i].label
              }
            }
            this.samplingOpinionDatas.push(item)
          })
          vm.loadFormData(re.data.data.opinions)
        } else {
          this.$Message.warning({
            title: '提示',
            content: re.data.data.loanId,
            onOk () {}
          })
        }
      }).catch(er => {})
    }
  },
  computed: {

  },
  mounted () {

  },
  created () {
    this.loanId = this.taskParams.curLoanId
    this.$ajax = this.$util.ajax()
    // this.initData();
  },
  watch: {
    // 点击tab页签，加载数据，只加载一次
    'taskParams.approvalResult' (v) {
      if (v) {
        this.loanId = this.taskParams.curLoanId
        this.initData()
      }
    }
  },
  props: {
    taskParams: Object
  }
}
</script>

<style>
.checkCard Row {
  border-bottom: 1px;
  margin: 0px 0px 5px 0px;
}

.f14 input {
  cursor: pointer;
}
</style>
